package com.erb.controller.hzg;

import com.erb.common.loanFileUtils;
import com.erb.constants.LoanConstants;
import com.erb.entity.*;
import com.erb.service.cgs.SmloadService;
import com.erb.service.hzg.LoanUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("loan/Personal")
public class LoanPersonal {

	@Resource
	private SmloadService smloadService;

	@Resource
	LoanUserService service;
	//上传文件
	@Resource
	loanFileUtils loanFileUtil;
	@Autowired
	private JavaMailSender mailSender;

	@RequestMapping("/preacover")
	public String preacover(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		System.out.println("用户的id信息为 ： " + user.getId());
		LoanUser loanUsers = service.lulist(user.getId());
		session.setAttribute("loanUsers", loanUsers);
		return "loan/alone/AccountOverview";
	}

	@RequestMapping("/preSafe")
	public String preSafe(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		System.out.println("用户的id信息为 ： " + user.getId());
		LoanUser loanUsers = service.lulist(user.getId());
		session.setAttribute("loanUsers", loanUsers);
		int card = service.setcard(user.getId());
		if (card >= 1) {
			session.setAttribute("showcard", "yes");
		} else {
			session.setAttribute("showcard", "no");
		}
		//持有车辆信息
		int cari = service.setcarinfo(user.getId());
		if (cari >= 1) {
			session.setAttribute("carlist", "yes");
		} else {
			session.setAttribute("carlist", "no");
		}
		System.out.println("持有的车辆数量为：" + cari);


		int housei = service.sethouseinfo(user.getId());
		if (housei >= 1) {
			session.setAttribute("houselist", "yes");
		} else {
			session.setAttribute("houselist", "no");
		}

		System.out.println("持有的房产数量为：" + housei);
		return "loan/alone/AccountSafe";
	}

	@RequestMapping("/pinggu")
	@ResponseBody
	public int pinggu(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		List<Map> selectcarimf = smloadService.selectcarimf(user.getId());
		List<Map> selecthouseimf = smloadService.selecthouseimf(user.getId());
		int xinxi=0;
		if(selectcarimf.size()==0){
			xinxi=1;
		}else if(selecthouseimf.size()==0){
			xinxi=2;
		}else {
			xinxi=3;
		}

		return xinxi;
	}

	@RequestMapping("/addassess")
	public String updassess(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		service.updassess(LoanConstants.Evaluation_Of_State, user.getId());
		return "redirect:preSafe";
	}

	//跳转充值金额
	@RequestMapping("/toBankmony")
	public String toBankmony(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanUser loanUsers = service.lulist(user.getId());
		session.setAttribute("loanUsers", loanUsers);
		return "/loan/alone/bankCZ";
	}

	@RequestMapping("/torelation")
	public String torelation(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanUser loanUsers = service.lulist(user.getId());
		session.setAttribute("loanUsers", loanUsers);
		return "/loan/alone/relation";
	}

	//充值金额
	@RequestMapping("/Bankmony")
	public String Bankmony(LoanBankcard bankcard, HttpSession session) {
		System.out.println("充值数据： " + bankcard.getBankmoney() + bankcard.getBankn());
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		int money = bankcard.getBankmoney();
		String bankn = bankcard.getBankn();
		if (money >= 0) {
			if (bankn != null && bankn != "") {
				LoanUser loanUser = service.lulist(user.getId());
				double lbmoney = loanUser.getAvailableLimit();
				double bmon = money + lbmoney;
				service.addbankmony(bmon, user.getId());
				return "redirect:preacover";
			}
		}
		return "redirect:toBankmony";
	}

	//跳转设置银行卡密码界面
	@RequestMapping("/toBcardPwd")
	public String toBcardPwd(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanUser loanUsers = service.lulist(user.getId());
		session.setAttribute("loanUsers", loanUsers);
		return "/loan/alone/bankCardPwd";
	}

	//跳转修改银行卡密码界面
	@RequestMapping("/toBupdPwd")
	public String toBupdPwd(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanUser loanUsers = service.lulist(user.getId());
		session.setAttribute("loanUsers", loanUsers);
		return "/loan/alone/updBcardPwd";
	}

	//获取实名认证数据
	@RequestMapping("/preapply")
	public String preapply(HttpSession session, Model model, Pager pager) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanUser loanUsers = service.lulist(user.getId());
		session.setAttribute("loanUsers", loanUsers);

		pager.page(service.selbankProcount(user.getId()));
		pager.data = service.selbankApplyPage(pager, user.getId());
		model.addAttribute("pager", pager);
		//获取银行卡信息
		List<LoanBankcard> bankcards = service.banklist(user.getId());
		model.addAttribute("banklists", pager.data);

		int card = service.setcard(user.getId());
		if (card >= 1) {
			session.setAttribute("showcards", "yes");
		} else {
			session.setAttribute("showcards", "no");
		}
		return "loan/alone/apply";
	}

	//获取信息
	@RequestMapping("/carinfo")
	public String carinfo(HttpSession session, Model model) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		System.out.println("用户的id信息为 ： " + user.getId());
		LoanUser loanUsers = service.lulist(user.getId());
		session.setAttribute("loanUsers", loanUsers);
		//车辆状态
		int cari = service.setcarinfo(user.getId());
		System.out.println("持有的车辆数量为：" + cari);
		session.setAttribute("carlistts", cari);

		//车辆信息
		List<Carimf> carimfList = service.carlist(user.getId());
		model.addAttribute("carimfList", carimfList);
		return "loan/alone/carapply";
	}

	@RequestMapping("/loancar")
	public String addloancar(Carimf carimf, MultipartFile file, HttpSession session) {
		if (!file.isEmpty()) {
			String filename = loanFileUtil.fileUpload(file, LoanConstants.CARimg_save);
			carimf.setCarprove("/" + filename);
		}
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		carimf.setUid(user.getId());
		carimf.setCarowner(LoanConstants.HOUSEOWNER_USER);
		carimf.setCarapplyid(0);
		//添加车辆
		service.addgrCar(carimf);
		return "redirect:selcarinfo";
	}

	//删除车辆
	@RequestMapping("/delgrcar")
	public String delgrcar(HttpSession session, String id) {
		service.delgrcar(Integer.parseInt(id));
		return "redirect:selcarinfo";
	}

	//查看车辆信息
	@RequestMapping("/selcarinfo")
	public String selcarinfo(HttpSession session, Model model, Pager pager) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		System.out.println("用户的id信息为 ： " + user.getId());
		LoanUser loanUsers = service.lulist(user.getId());
		session.setAttribute("loanUsers", loanUsers);

		pager.page(service.selcarProcount(user.getId()));
		pager.data = service.selcarApplyPage(pager, user.getId());
		model.addAttribute("pager", pager);
		//车辆信息
		List<Carimf> carimfList = service.carlist(user.getId());
		model.addAttribute("carimfList", pager.data);
		return "loan/alone/carapplist";
	}


	//获取实房产数据
	@RequestMapping("/houseinfo")
	public String houseinfo(HttpSession session, Model model, Pager pager) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		System.out.println("用户的id信息为 ： " + user.getId());
		LoanUser loanUsers = service.lulist(user.getId());
		session.setAttribute("loanUsers", loanUsers);

		int housei = service.sethouseinfo(user.getId());
		session.setAttribute("houselist", housei);


		pager.page(service.selhouseProcount(user.getId()));
		pager.data = service.selhouseApplyPage(pager, user.getId());
		model.addAttribute("pager", pager);
		//房信息
		List<Houseimf> houseimfs = service.Houselist(user.getId());
		model.addAttribute("houseimfs", pager.data);
		return "loan/alone/houseApplist";
	}

	@RequestMapping("/tohouseadd")
	public String tohouseadd(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		System.out.println("用户的id信息为 ： " + user.getId());
		LoanUser loanUsers = service.lulist(user.getId());
		session.setAttribute("loanUsers", loanUsers);
		return "loan/alone/houseApply";
	}

	//添加房产
	@RequestMapping("/addloanHouse")
	public String addloanHouse(Houseimf houseimf, MultipartFile file, HttpSession session) {
		if (!file.isEmpty()) {
			String filename = loanFileUtil.fileUpload(file, LoanConstants.HouseImg_save);
			houseimf.setHouseprove("/" + filename);
		}
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		houseimf.setUid(user.getId());
		houseimf.setHouseowner(LoanConstants.HOUSEOWNER_USER);
		houseimf.setHouseapplyid(0);
		service.addgrHouse(houseimf);
		return "redirect:houseinfo";
	}

	//删除房
	@RequestMapping("/delgrHouse")
	public String delgrHouse(HttpSession session, String id) {
		service.delgrHouse(Integer.parseInt(id));
		return "redirect:houseinfo";
	}

	//修改状态
	@RequestMapping("/updbankst")
	public String updbankst(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		session.setAttribute("showcards", "no");
		return "loan/alone/apply";
	}

	//删除数据
	@RequestMapping("/delbankst")
	public String delbankst(HttpSession session, String id) {
		service.delbankst(Integer.parseInt(id));
		return "redirect:preapply";
	}

	//添加银行卡
	@RequestMapping("/applyupd")
	@ResponseBody
	public String applyupd(String uName, String cardId, String bankName, String identity, HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanBankcard bankcard = new LoanBankcard();
		bankcard.setLuid(user.getId());
		bankcard.setBankn(cardId);
		bankcard.setBankname(bankName);
		bankcard.setReservedname(uName);
		bankcard.setBankmoney(0);
		service.addbank(bankcard);
		//添加身份证
		service.addidcard(identity, LoanConstants.Autonym_State_yes, user.getId());
		session.setAttribute("forapply", "yes");
		return "1";
	}

	//添加银行卡密码
	@RequestMapping("/applyupdpwd")
	@ResponseBody
	public String applyupdpwd(String uName, HttpSession session) {
		System.out.println("银行卡密码：" + uName);
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		int i = service.updPwd(uName, user.getId());
		System.out.println("用户id：" + user.getId());
		String zhi = "失败";
		if (i > 0) {
			return "成功";
		}
		return "失败";
	}

	//判断原密码是否相同
	//修改银行卡密码
	@RequestMapping("/showpwd")
	@ResponseBody
	public int showpwd(String ympwd, HttpSession session) {
		System.out.println("输入密码：" + ympwd);
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanUser user1 = service.lulist(user.getId());
		System.out.println("数据库密码：" + user1.getBankpwd());
		if (ympwd == user1.getBankpwd() || ympwd.equals(user1.getBankpwd())) {
			return 1;
		} else if (user1.getBankpwd() == null) {
			return 3;
		}
		return 0;
	}

	//判断号是否相同
	@RequestMapping("/showbankkn")
	@ResponseBody
	public int showbankkn(String ympwd, HttpSession session) {
		System.out.println("输入密码：" + ympwd);
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanBankcard bankcard = service.showbanknMoney(ympwd, user.getId());
		if (bankcard != null) {
			return 1;
		}
		return 0;
	}

	//添加紧急联系人
	@RequestMapping("/relations")
	public String relations(LoanUser loanUser, HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		loanUser.setId(user.getId());
		System.out.println(loanUser.getLoanname1());
		System.out.println(loanUser.getPhone1());
		System.out.println(loanUser.getRelation());
		service.addrelat(loanUser);
		return "redirect:preSafe";
	}

	//邮件发送实现
	@RequestMapping("/sendEmail")
	@ResponseBody
	public int sendEmail(String emailcode, String myEmail) {
		try {
			MimeMessage maMessage = mailSender.createMimeMessage();
			// 不添加，文件名会不正常显示
			MimeMessageHelper helper = new MimeMessageHelper(maMessage, true);
			helper.setFrom("huazgen@163.com");//表明是谁发送的
			helper.setTo(myEmail);//说明这是发送给谁的
			helper.setSubject("验证码邮件");//邮件的标题
			helper.setText("这是验证码啊：" + emailcode);//邮件的内容内容
			mailSender.send(maMessage);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

}
